package com.my.study.controller;

import com.my.study.entity.MyUserEntity;
import com.my.study.service.MyUserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBuckets;
import org.redisson.api.RKeys;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Carlos
 * @version 1.0.0
 * @date 2022/1/7 8:37
 */
@Slf4j
@RestController
public class UserLoginController {

    @Autowired
    private MyUserService myUserService;
    
    @Resource
    private RedissonClient redissonClient;

    @PostMapping(value = "/register")
    public Object registerUser(MyUserEntity myUser) {
        myUserService.registerUser(myUser);
        return null;
    }

    @GetMapping("/login/oauth2/code/gitee")
    public String gitee(HttpServletRequest request, @RequestParam(value = "code") String code) {
        log.info("code: {}", code);
        return code;
    }
    
    // @PreAuthorize(value = "@userLoginController.isLen(#len)")
    //@PreAuthorize(value = "hasAuthority('sys:find')")
    //@PreAuthorize(value = "hasAnyRole('admin')")
    @GetMapping("/ok")
    public String getOk(Integer len) {
//        if (null != len && len >= 4) {
//            RKeys keys = redissonClient.getKeys();
//            keys.getKeys().forEach(key -> {
//                log.info("- key: {}", key);
//            });
//            keys.deleteByPattern("spring:security:rememberMe:*");
//        }
        
        return "ok";
    }

    
    public boolean isLen(Integer len) {
        return len > 3;
    }
    
//    @GetMapping("/api/callback")
//    public String callback(HttpServletRequest request) {
//        System.out.println(request);
//        return "callback";
//    }
    
    @GetMapping("/getUser")
    public MyUserEntity getUser(Integer userId) {
        return myUserService.getUser(userId);
    }
    
}
